package designpattern.proxy.impl;

import org.apache.log4j.Logger;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author 吴阳基
 * @datetime 2024/1/6 9:47
 */
public class IntermediaryInvocationHandler implements InvocationHandler {
    private Logger logger = Logger.getLogger(IntermediaryInvocationHandler.class);
    /**
     * 被代理的对象，也就是被代理的目标对象。
     */
    private Object target;

    public void setTarget (Object target) {
        this.target = target;
    }

    /**
     * 实现代理的方法。
     * @param proxy the proxy instance that the method was invoked on
     *
     * @param method the {@code Method} instance corresponding to
     * the interface method invoked on the proxy instance.  The declaring
     * class of the {@code Method} object will be the interface that
     * the method was declared in, which may be a superinterface of the
     * proxy interface that the proxy class inherits the method through.
     *
     * @param args an array of objects containing the values of the
     * arguments passed in the method invocation on the proxy instance,
     * or {@code null} if interface method takes no arguments.
     * Arguments of primitive types are wrapped in instances of the
     * appropriate primitive wrapper class, such as
     * {@code java.lang.Integer} or {@code java.lang.Boolean}.
     *
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object feedback =  method.invoke(target, args);
        after();
        return "看房记录：买家反馈" + feedback;
    }
    private void before(){
        logger.info("买家进入看房记录");
    }
    private void after(){
        logger.info("买家离开看房记录");
    }
}
